package com.situ.day13;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;

public class ReflectDemo {

	@Test
	public void testClass() throws ClassNotFoundException {
		//1 Class.forName(类路径)
		Class clazz1 = Class.forName("com.situ.day13.Student");
		//2 类型.class
		Class clazz2 = Student.class;
		//3 对象.getClass
		Student student = new Student();
		Class clazz3 = student.getClass();
		
		System.out.println(clazz1 == clazz2);
		System.out.println(clazz2 == clazz3);
	}
	
	@Test
	public void testConstructor1() {
		Class clazz = Student.class;
		Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
		for (Constructor constructor : declaredConstructors) {
			System.out.println(constructor);
			//获取修饰符
			System.out.println(constructor.getModifiers());
			System.out.println(constructor.getName());
		}
	}
	
	@Test
	public void testConstructor2() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Class clazz = Student.class;
		Constructor constructor = clazz.getDeclaredConstructor(String.class);
		//Student student = new Student("zhangsan");
		//如果构造方法为私有类型 用下面这条语句
		//constructor.setAccessible(true);
		Student student = (Student) constructor.newInstance("zhangsan");
		System.out.println(student);
	}
	
	
}
